package com.jss.lanouhn.fristapp.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.CardView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jss.lanouhn.fristapp.R;
import com.jss.lanouhn.fristapp.activity.SearchActivity;
import com.jss.lanouhn.fristapp.activity.SearchContentActivity;
import com.jss.lanouhn.fristapp.adapters.ViewPagerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * Created by lanouhn on 16/8/23.
 */
public class Fragment_Category extends Fragment{
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private List<String> tabName;
    private List<Fragment> fragments;
    private TextView tv_seacher;
    private CardView cardView;



    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_category,null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        tabLayout= (TabLayout) getView().findViewById(R.id.tl_bar_title_category);
        viewPager= (ViewPager) getView().findViewById(R.id.vp_category);
        tv_seacher= (TextView) getView().findViewById(R.id.tv_bar_seacher_category);
        cardView= (CardView) getView().findViewById(R.id.cv_search);
        cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().startActivity(new Intent(getActivity(), SearchActivity.class));
            }
        });
        initDate();
    }

    private void initDate() {
        tabName=new ArrayList<String>();
        tabName.add("攻略");
        tabName.add("单品");
        fragments=new ArrayList<Fragment>();

        fragments.add(new Category_Strategy_Fragment());
        fragments.add(new Category_Sample_Fragment());
        viewPager.setAdapter(new ViewPagerAdapter(getChildFragmentManager(),fragments,tabName));
        tabLayout.setupWithViewPager(viewPager);
        int n=viewPager.getCurrentItem();
        Log.d("sample","谁的卡上"+n);

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if(position==1) {
                    tv_seacher.setVisibility(View.VISIBLE);
                    tv_seacher.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            getActivity().startActivity(new Intent(getActivity(), SearchContentActivity.class));
                        }
                    });
                }else{
                    tv_seacher.setVisibility(View.INVISIBLE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });





    }
}
